package com.yanyeori.singleton;

/**
 * @author chenkuan
 * @desc 懒汉模式
 * @date 2021/4/9 009 16:51
 */
public class LazySingleton {
    /**
     * volatile 防止指令重排序
     */
    private static volatile LazySingleton instance;

    private LazySingleton() {
    }

    /**
     * public synchronized static LazySingleton getInstance() {} 方式也可实现，但影响性能
     */
    public static LazySingleton getInstance() {
        if (instance == null) {
            synchronized (LazySingleton.class) {
                if (instance == null) {
                    instance = new LazySingleton();
                }
            }
        }
        return instance;
    }
}
